package com.sky.service.impl;

import com.sky.constant.StatusConstant;
import com.sky.entity.Dish;
import com.sky.entity.Orders;
import com.sky.mapper.*;
import com.sky.service.WorkspaceService;
import com.sky.vo.BusinessDataVO;
import com.sky.vo.DishOverViewVO;
import com.sky.vo.OrderOverViewVO;
import com.sky.vo.SetmealOverViewVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.HashMap;
import java.util.Map;

@Service
public class WorkspaceServiceImpl implements WorkspaceService {
    @Autowired
    private OrderMapper orderMapper;

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private DishMapper dishMapper;

    @Autowired
    private SetmealMapper setmealMapper;

    //查询今日运营数据
    @Override
    @Transactional
    public BusinessDataVO businessData(LocalDateTime begin,LocalDateTime end) {
        //营业额
        Double turnover = orderMapper.turnoverStatistics(begin.with(LocalTime.MIN), end.with(LocalTime.MAX));
        turnover=turnover==null?0:turnover;

        //有效订单数
        Map map = new HashMap<>();
        map.put("begin",begin.with(LocalTime.MIN));
        map.put("end",end.with(LocalTime.MAX));
        map.put("status", Orders.COMPLETED);
        Integer validOrderCount = orderMapper.ordersStatistics(map);
        validOrderCount=validOrderCount==null?0:validOrderCount;

        //订单完成率
        map.remove("status");
        Integer orderCount = orderMapper.ordersStatistics(map);
        Double orderCompletionRate = validOrderCount/(double) orderCount;
        orderCompletionRate=orderCount==0?0.0:orderCompletionRate;

        //平均客单价
        Double unitPrice = turnover/(double) orderCount;
        unitPrice=orderCount==0?0.0:unitPrice;

        //新增用户数
        Integer newUsers = userMapper.userStatistics(map);
        newUsers=newUsers==null?0:newUsers;

        return BusinessDataVO
                .builder()
                .turnover(turnover)
                .validOrderCount(validOrderCount)
                .orderCompletionRate(orderCompletionRate)
                .unitPrice(unitPrice)
                .newUsers(newUsers)
                .build();
    }

    //查询菜品总览
    @Override
    @Transactional
    public DishOverViewVO overviewDishes() {
        Integer sold = dishMapper.overviewDishes(StatusConstant.ENABLE);
        Integer discontinued = dishMapper.overviewDishes(StatusConstant.DISABLE);
        return DishOverViewVO
                .builder()
                .sold(sold)
                .discontinued(discontinued)
                .build();
    }

    //查询套餐总览
    @Override
    @Transactional
    public SetmealOverViewVO overviewSetmeals() {
        Integer sold = setmealMapper.overviewSetmeals(StatusConstant.ENABLE);
        Integer discontinued = setmealMapper.overviewSetmeals(StatusConstant.DISABLE);
        return SetmealOverViewVO
                .builder()
                .sold(sold)
                .discontinued(discontinued)
                .build();
    }

    //查询订单管理数据
    @Override
    @Transactional
    public OrderOverViewVO overviewOrders() {
        Integer waitingOrders = orderMapper.orderStatistics(Orders.TO_BE_CONFIRMED);
        Integer deliveredOrders = orderMapper.orderStatistics(Orders.CONFIRMED);
        Integer completedOrders = orderMapper.orderStatistics(Orders.COMPLETED);
        Integer cancelledOrders = orderMapper.orderStatistics(Orders.CANCELLED);
        Integer allOrders = orderMapper.orderStatistics(null);

        return OrderOverViewVO
                .builder()
                .waitingOrders(waitingOrders)
                .deliveredOrders(deliveredOrders)
                .cancelledOrders(cancelledOrders)
                .completedOrders(completedOrders)
                .allOrders(allOrders)
                .build();
    }
}
